package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("login/{username}/{password}")
    public JsonResult doLogin(@PathVariable String username,
                              @PathVariable String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        token.setRememberMe(true);
        subject.login(token);
//        return new JsonResult("login ok");
        JsonResult jr = new JsonResult("登录ok");
        jr.setData(username);
        return jr;
    }

    //    @RequiresPermissions("sys:user:update")
    @PatchMapping("{id}/{valid}")
    public JsonResult updateValid(@PathVariable Integer id,
                                  @PathVariable Integer valid) {
        sysUserService.updateValid(id, valid);
        return new JsonResult("valid update ok");
    }

    @PostMapping
    public JsonResult doSaveUser(@RequestBody SysUser sysUser) {
        sysUserService.saveUser(sysUser);
        return new JsonResult("save ok");
    }

    @PutMapping
    public JsonResult doUpdateUser(@RequestBody SysUser sysUser) {
        sysUserService.updateUser(sysUser);
        return new JsonResult("update ok");
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Integer id) {
        return new JsonResult(sysUserService.findById(id));
    }

    @GetMapping
    public JsonResult doFindUsers(SysUser sysUser) {
        return new JsonResult(sysUserService.findUsers(sysUser));
    }
}
